package com.dsit.sq.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dsit.sq.App;
import com.dsit.sq.R;

/**
 * Created by dsit_dev04 on 2016/10/17.
 */
public class WifiStatusService extends Service {

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private LayoutInflater inflater = null;
    private RelativeLayout wifiLayout = null;
    private TextView wifiTextView = null;
    ConnectivityManager connMgr ;
    WifiManager wifiManager;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    boolean initViewPlace = false;

    Handler updateWifi = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message message) {
            if (message.what == 0) {
                if (App.instance.deviceConnected) {
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    NetworkInfo info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    if (info.isConnected()) {
                        int level = wifiInfo.getRssi();
                        // 根据获得的信号强度发送信息
                        if (level <= 0 && level >= -50) {
                            // 信号最强
                            wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_05);
                        } else if (level < -50 && level >= -70) {
                            //
                            wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_04);
                        } else if (level < -70 && level >= -80) {
                            //
                            wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_03);
                        } else if (level < -80 && level >= -100) {
                            //
                            wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_02);
                        } else {
                            // 小于-200，一般已经断开
                            wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_01);
                        }
                    }
                } else {
                    wifiTextView.setBackgroundResource(R.mipmap.btn_wifi_no);
                }
            }
            sendEmptyMessageDelayed(0,2000);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        windowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        inflater = LayoutInflater.from(getApplication());
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        layoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE ;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
        layoutParams.y = 120;
        layoutParams.x = 800;
        wifiLayout = (RelativeLayout) inflater.inflate(R.layout.layout_wifi_service, null);
        wifiTextView = (TextView) wifiLayout.findViewById(R.id.wifi_icon);
        windowManager.addView(wifiLayout, layoutParams);
        updateWifi.sendEmptyMessage(0);
        wifiLayout.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (!initViewPlace) {
                            initViewPlace = true;
                            //获取初始位置
                            mTouchStartX = event.getRawX();
                            mTouchStartY = event.getRawY();
                            x = event.getRawX();
                            y = event.getRawY();
                            Log.i("service", "startX:" + mTouchStartX + "=>startY:" + mTouchStartY);
                        }else {
                            //根据上次手指离开的位置与此次点击的位置进行初始位置微调
                            mTouchStartX += (event.getRawX() -x);
                            mTouchStartY += (event.getRawY() -y);
                        }

                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 获取相对屏幕的坐标，以屏幕左上角为原点
                        x = event.getRawX();
                        y = event.getRawY();
                        updateViewPosition();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
    }

    private void updateViewPosition() {
        // 更新浮动窗口位置参数
        layoutParams.x = (int) (x - mTouchStartX);
        layoutParams.y = (int) (y - mTouchStartY);
        windowManager.updateViewLayout(wifiLayout, layoutParams);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        windowManager.removeView(wifiLayout);
    }
}
